﻿using furion.blog.Domain.IRepositories;
using furion.blog.Domain.MySQL;
using furion.blog.Domain.Query.Member;
using furion.blog.Domain.Shared;
using furion.blog.Domain.Shared.Dto.Member;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.EntityFramework.Core.Repositories
{
    public class MemberRepository : BaseSqlSugarRepository<Member>, IMemberRepository, ISingleton
    {

        public readonly ISqlSugarClient _db;

        public MemberRepository(ISqlSugarClient db)
        {
            this._db = db;
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public async Task<Member> First(Expression<Func<furion.blog.Domain.MySQL.Member, bool>> exp) { 
           return await base.GetFirstAsync(exp);    
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public async Task<bool> Save(Member member) {
            return await base.InsertAsync(member);
        }


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageVO> List(MemberListQuery query) {
            try
            {
                var (strWhere, queryParams) = query.QueryParams();
                RefAsync<int> totalRecord = 0;
                string columns = string.Format(@"
t.member_id as memberId, t.phone as Phone , t.real_name as RealName ,t.nick_name as NickName,
t.integral as Integral,t.sex as Sex,t.birthday as Birthday,t.email as Email,t.mini_program_openid as MiniProgramOpenId,
t.created_time as createdTime,t.is_delete as isDelete,t.status as status,t.location as Location
");
                var list = await _db.SqlQueryable<object>
                    (@" select " + columns + " from `member` t ")
                    .Where(strWhere)
                    .AddParameters(queryParams)
                    .OrderBy(" t.createdTime desc ")
                    .ToPageListAsync(query.PageIndex, query.PageSize, totalRecord);
                return new PageVO { Records = list, Total = totalRecord };
            }
            catch (Exception ex)
            {
                throw Oops.Oh(ex.Message);
            }
        }


        /// <summary>
        /// 更新会员地理位置信息
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public async Task UpdateLocation(long memberId, string location) {
            //只更新location列
            await base.UpdateAsync(it => new Member() { Location = location /*可以多列*/ }, it => it.MemberId == memberId);
        }


        /// <summary>
        /// 通过ID查找实体
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public async Task<Member> FindById(long memberId) {
            return await base.GetByIdAsync(memberId);
        }


        /// <summary>
        /// 通过手机号码查找
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public async Task<Member> FindByPhone(string phone) {
            return await base.GetFirstAsync(it=>it.Phone == phone);
        }


        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task UpdateProfile(MemberProfileUpdateDto dto) {
            await base.UpdateAsync(it=>new Member() 
            { 
              Avatar = dto.Avatar,
              NickName = dto.NickName,
              RealName = dto.RealName,
              Sex = dto.Sex,
              Birthday = dto.Birthday,
              Email = dto.Email,
              Phone = dto.Phone,
              UpdatedTime = DateTime.Now,
              Wechat = dto.Wechat,
            },it=>it.MemberId == dto.MemberId);
        }


        /// <summary>
        /// 更新手机号码
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public async Task<bool> UpdatePhone(long memberId, string phone) {
            //只更新phone列
            var ret = await base.UpdateAsync(it => new Member() { 
                Phone = phone , 
                UpdatedTime = DateTime.Now 
            }, it => it.MemberId == memberId);
            return ret;
        }

    }
}
